package com.xdemo.personal.handler;

import com.xdemo.core.MessageType;
import com.xdemo.core.msg.TestExchangeMessage;
import com.xdemo.handler.IMessageAction;
import com.xdemo.manager.IAsyncManager;
import com.xdemo.manager.ICachedTimeManager;
import com.xdemo.msg.IMessage;
import com.xdemo.personal.async.QueryWork;
import com.xdemo.personal.async.StoreWork;
import com.xdemo.personal.communityclient.ICommunityServerManager;
import com.xdemo.personal.server.pool.EManager;
import com.xdemo.personal.server.pool.ManagerPool;
import com.xdemo.personal.socketserver.msg.QueryResultMessage;
import com.xdemo.personal.socketserver.msg.StoreMessage;

public class TestMessageAction implements IMessageAction<Integer> {

	private final static Integer[] TYPES = {MessageType.Query,MessageType.Store,MessageType.TestSchedule};
	
	@Override
	public void doAction(IMessage<Integer> message) {
		switch(message.getType()) {
		case MessageType.Query:{
			QueryResultMessage _msg = (QueryResultMessage)message;
			System.out.println("query message action:" + _msg.getKey());
			IAsyncManager _manager = (IAsyncManager)ManagerPool.getManager(EManager.Async);
			_manager.executeWork(new QueryWork(_msg.getPlayer(),_msg.getKey()));
			break;
		}
		case MessageType.Store: {
			StoreMessage _msg = (StoreMessage)message;
			System.out.println("store message action:" + _msg.getStoreKey());
			IAsyncManager _manager = (IAsyncManager)ManagerPool.getManager(EManager.Async);
			_manager.executeWork(new StoreWork(_msg.getPlayer(),_msg.getStoreKey()));
			break;
		}
		case MessageType.TestSchedule: {
			ICachedTimeManager _timerManager = (ICachedTimeManager)ManagerPool.getManager(EManager.Time);
			_timerManager.update();
			ICommunityServerManager _communityManager = (ICommunityServerManager)ManagerPool.getManager(EManager.Community);
			_communityManager.sendMessage(new TestExchangeMessage());
			System.out.println("update system time!");
			break;
		}
		}
	}

	@Override
	public Integer[] getTypes() {
		return TYPES;
	}

}
